﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

public partial class MRP_ReturnProduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void UIServer1_CmdFieldFire(object sender, Evolutility.UIServer.CmdFieldEventArgs e)
    {
        int id = Int32.Parse(e.PK);
        string sSQL = "";
        List<OrderMaterial> MaterialList = new List<OrderMaterial>();

        if (e.CommandText.Equals("发布"))
        {
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");

            sSQL = string.Format("Update MRP_Order set [status]=2 where ID={0}", id);
            db.ExecuteNonQuery(CommandType.Text, sSQL);

            MRPUtility.WriteGeneralLog(String.Format("发布了订单（ID={0}），该订单已经被锁定！",id));

            sSQL = string.Format("SELECT MRP_OrderDetail.ProductID,MRP_OrderDetail.Quantity FROM MRP_Order INNER JOIN MRP_OrderDetail ON MRP_Order.ID = MRP_OrderDetail.OrderID where MRP_Order.ID={0}", id);
            //取得MaterialID
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, sSQL))
            {
                while (reader.Read())
                {
                    MaterialList.Add(new OrderMaterial(int.Parse(reader[0].ToString()), int.Parse(reader[1].ToString())));
                }

            }

            foreach (OrderMaterial Material in MaterialList)
            {
                sSQL = string.Format("Update MRP_Stock set InTransit=InTransit+{1} where MaterialID={0}", Material.MaterialID, Material.Count);

                db.ExecuteNonQuery(CommandType.Text, sSQL);

                MRPUtility.WriteGeneralLog(String.Format("产品（ID={0}）的在途数量增加了{1}", Material.MaterialID, Material.Count));
            }
        }
    }
}
